<?php
// Include the file
// TODO: more import content
// Just need add library at this place
/**
 */
require_once(ABSPATH . "modules/myclass.inc.php");
require_once(ABSPATH . "modules/student.inc.php");
require_once(ABSPATH . "modules/sport.inc.php");
require_once(ABSPATH . "modules/role.inc.php");
require_once(ABSPATH . "modules/teacher.inc.php");
/**
 */
/**
 * initImportItem()
 *
 * @return
 */
function initImportItem()
{
    $items = array(
        array("action" => 'import_student', "name" => _e("Student's list"), "desc" => _e("Import student's list from csv file.")),
        array("action" => 'import_teacher', "name" => _e("Teacher's list"), "desc" => _e("Import teacher's list from csv file.")),
        array("action" => 'import_myclass', "name" => _e("Class's list"), "desc" => _e("Import class's list from csv file.")),
        array("action" => 'import_arrangement', "name" => _e("Arrangement's list"), "desc" => _e("Import arrangement's list from csv file."))
        );
    return $items;
}
/**
 * showImportForm()
 *
 * @return
 */
function showImportForm()
{
    switch ($_GET['action']) {
        case 'import_student':
            require_once(ABSPATH . "modules/import_student.php");
            break;
        case 'import_teacher':
            require_once(ABSPATH . "modules/import_teacher.php");
            break;
        case 'import_myclass':
            require_once(ABSPATH . "modules/import_myclass.php");
            break;
        case 'import_arrangement':
            require_once(ABSPATH . "modules/import_arrangement.php");
            break;
        default: ;
            break;
    } // switch
}

/**
 * importStudentList()
 *
 * @param string $sListFile
 * @param string $sClassId
 * @return
 */
function importStudentList($sListFile = "", $sClassId = "")
{
    $fh = fopen($sListFile, "r");
    while ($line = fgetcsv($fh, "1000", ",")) {
        $new_no = createNewStudentNo($sClassId);
        $student_list[] = array(":student_name" => $line[0], ":student_no" => $new_no);
        $student_info[] = array(":student_no" => $new_no,
            ":sex_id" =>$line[1],
            ":myclass_id" => serialize($sClassId),
            ":is_enable" => 1
            );
    }
    // var_dump($student_list);
    $sql = "REPLACE INTO " . DB_PREFIX . "student SET student_name = :student_name,student_no =:student_no";
    zeus_sql_prepare($sql, false, $student_list);
    $sql = "REPLACE INTO " . DB_PREFIX . "rel_student_info SET student_no = :student_no,myclass_id =:myclass_id,sex_id =:sex_id,is_enable=:is_enable";
    zeus_sql_prepare($sql, false, $student_info);
    fclose($fh);
    return true;
}
/**
 * importTeacherList()
 *
 * @param string $sListFile
 * @return
 */
function importTeacherList($sListFile = "")
{
    $fh = fopen($sListFile, "r");
    $pwd = md5("yuss@" . date("Y"));
    $role_id = "4";
    $last_user_id = getLatestUserId();
    while ($line = fgetcsv($fh, "1000", ",")) {
        $new_no = createNewTeacherNo();
        $teacher_name = $line[0];
        $cell_phone = $line[1];
        $data['cell_phone'] = $cell_phone;
        $detail = serialize($data);
        $teacher_list[] = array(":teacher_name" => $teacher_name, ":teacher_no" => $new_no);
        $teacher_info[] = array(":teacher_no" => $new_no,
            ":is_enable" => 1,
            ":detail" => $detail
            );
        $user_auth[] = array(":user_name" => $new_no,
            ":user_pwd" => $pwd,
            "role_id" => $role_id,
            ":is_enable" => 1
            );
        $user_detail[] = array(":user_id" => ++$last_user_id,
            ":cell_phone" => $cell_phone,
            ":signup_time" => date("Y-m-d h:i:s"),
            ":real_name" => $teacher_name
            );
    }
    $sql = "REPLACE INTO " . DB_PREFIX . "teacher SET teacher_name = :teacher_name,teacher_no=:teacher_no";
    zeus_sql_prepare($sql, false, $teacher_list);
    $sql = "REPLACE INTO " . DB_PREFIX . "rel_teacher_info SET teacher_no=:teacher_no,
    is_enable = :is_enable,
    detail = :detail
	";
    zeus_sql_prepare($sql, false, $teacher_info);
    $sql = "REPLACE INTO " . DB_PREFIX . "user_auth SET user_name = :user_name,
    user_pwd = :user_pwd,
    role_id = :role_id,
    is_enable=:is_enable
	";
    zeus_sql_prepare($sql, false, $user_auth);
    $sql = "REPLACE INTO " . DB_PREFIX . "user_detail SET user_id =:user_id,
	cell_phone = :cell_phone,
	real_name = :real_name,
	signup_time=:signup_time";
    zeus_sql_prepare($sql, false, $user_detail);
    fclose($fh);
    return true;
}
/**
 * importClassList()
 *
 * @param string $sListFile
 * @return
 */
function importClassList($sListFile = "")
{
    $sql = "REPLACE INTO " . DB_PREFIX . "myclass SET myclass_name = :myclass_name,is_enable=:is_enable,graduate_time=:graduate_time";
    $fh = fopen($sListFile, "r");
    while ($line = fgetcsv($fh, "1000", ",")) {
        $myclass_list[] = array(":myclass_name" => $line[0],
            ":is_enable" => 1,
            ":graduate_time" => substr($line[0], 0, 4) . "-06-30 12:00:00"
            );
    }
    zeus_sql_prepare($sql, false, $myclass_list);
    fclose($fh);
    return true;
}

/**
 * importArrangementList()
 *
 * @param string $sListFile
 * @return
 */
function importArrangementList($sListFile = "")
{
    $sql = "UPDATE " . DB_PREFIX . "rel_teacher_info SET myclasses=:myclasses WHERE teacher_no=:teacher_no";
    $fh = fopen($sListFile, "r");
    while ($line = fgetcsv($fh, "1000", ",")) {
        $teacher_no = $line[0];
        $currentYear = date("Y");
        $classes = getTheTeacherClasses($teacher_no);
        $classes[$currentYear] = $line[2];
        $myclasses = serialize($classes);
        $info[] = array(":myclasses" => $myclasses,
            ":teacher_no" => $teacher_no
            );
    }
    zeus_sql_prepare($sql, false, $info);
    fclose($fh);
    return true;
} 
?>